/**
 * @fileoverview jsx字符串需使用多语言转化
 * @author 
 */
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
    meta: {
        docs: {
            description: "jsx字符串需使用多语言转化",
            category: "Fill me in",
            recommended: false
        },
        fixable: null,  // or "code" or "whitespace"
        schema: [
            // fill in your schema
        ]
    },

    create: function(context) {

        // variables should be defined here

        //----------------------------------------------------------------------
        // Helpers
        //----------------------------------------------------------------------

        // any helper functions should go here or else delete this section

        //----------------------------------------------------------------------
        // Public
        //----------------------------------------------------------------------

        const {
            parserServices,
            options: [
              {
                onlyAttribute = [],
                markupOnly: _markupOnly,
                validateTemplate,
                ignoreComponent = [],
                ignoreAttribute = [],
                ignoreProperty = [],
                ignoreCallee = [],
                ignore = [],
              } = {},
            ],
          } = context;
          const whitelists = [
            /^[0-9!-/:-@[-`{-~]+$/, // ignore not-word string
            ...ignore,
          ].map(item => new RegExp(item));
      
          const message = '不允许直接在jsx中使用字符串';
      
          //----------------------------------------------------------------------
          // Public
          //----------------------------------------------------------------------
          const visited = new WeakSet();
      
      
          function isString(node) {
            return typeof node.value === 'string';
          }
      
          const { esTreeNodeToTSNodeMap, program } = parserServices;
          let typeChecker;
          if (program && esTreeNodeToTSNodeMap)
            typeChecker = program.getTypeChecker();
      
          function isValidLiteral(str) {
            const trimed = str.trim();
            if (!trimed) return true;
      
            // allow statements like const a = "FOO"
            if (isUpperCase(trimed)) return true;
      
            if (match(trimed)) return true;
          }
      
          function validateLiteralNode(node) {
            // make sure node is string literal
            if (!isString(node)) return;
            if (isValidLiteral(node.value)) {
              return;
            }
      
            //
            // TYPESCRIPT
            //
      
            if (typeChecker) {
              const tsNode = esTreeNodeToTSNodeMap.get(node);
              const typeObj = typeChecker.getTypeAtLocation(tsNode.parent);
      
              // var a: 'abc' = 'abc'
              if (typeObj.isStringLiteral()) {
                return;
              }
      
              // var a: 'abc' | 'name' = 'abc'
              if (typeObj.isUnion()) {
                const found = typeObj.types.some(item => {
                  if (item.isStringLiteral() && item.value === node.value) {
                    return true;
                  }
                });
                if (found) return;
              }
            }
            // • • • • •
      
            context.report({ node, message });
          }
      
          // onlyAttribute would turn on markOnly
          const markupOnly = _markupOnly || !!onlyAttribute.length;
      
          function endIndicator() {
            indicatorStack.pop();
          }
      
          const scriptVisitor = {
            //
            // ─── JSX ─────────────────────────────────────────────────────────
            //
      
            JSXElement(node) {
              console.log('jsxE')
            },
      
            'JSXElement > Literal'(node) {
              console.log('jsxL')
              scriptVisitor.JSXText(node);
            },
      
            'JSXFragment > Literal'(node) {
              scriptVisitor.JSXText(node);
            },
      
            // @typescript-eslint/parser would parse string literal as JSXText node
            JSXText(node) {
            const trimed = node.value.trim();
              if (!trimed) {
                return;
              }
              context.report({ node, message });
            },
            // ─────────────────────────────────────────────────────────────────
      
          };
      
          return (
            scriptVisitor
          );
    }
};
